﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using KingRoad.EPM.InvestmeIBLL;
//using KingRoad.EPM.InvestmeEntity;
//using KingRoad.EPM.InvestmeBLL;
using KingRoad.EPM.ProjectInfoIBLL;
using KingRoad.EPM.ProjectInfoBLL;
using KingRoad.EPM.ProjectInfoEntity;

namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    /// <summary>
    /// ProjrSiteController
    /// 系统编码库维护
    /// 修改记录
    /// 
    /// 版本号 2.2
    /// <author>
    /// <name>CYL</name>
    /// <Data>2012.08.01</Data>
    /// </author>
    /// </summary>
    public class SystemCodeController : Controller
    {
        //
        // GET: /Permission/SystemCode/
        private IProject_SystemCodeBLL _SystemCodeBLL = null;
        public SystemCodeController()
        {
            this._SystemCodeBLL = new Project_SystemCodeBLL();
        }
        /// <summary>
        /// 主页试图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取zTree绑定数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetzTreeDB()
        {
            List<PROJECT_SYSTEMCODE> templist = null;
            templist = _SystemCodeBLL.GetAlllist();
            if (templist == null || templist.Count == 0)
            {
                PROJECT_SYSTEMCODE entity = new PROJECT_SYSTEMCODE()
                                              {
                                                  SYSTEMCODE_NAME = "系统编码库",
                                                  SYSTEMCODE_PARENTID = 0,
                                                  SYSTEMCODE_NODELEVEL = 0
                                              };
                _SystemCodeBLL.AddSystemCode(entity);
                templist = _SystemCodeBLL.GetAlllist();
            }
            return Json(templist);
        }
        /// <summary>
        /// 添加、编辑视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Create(int id)
        {
            PROJECT_SYSTEMCODE detail = new PROJECT_SYSTEMCODE() { SYSTEMCODE_PARENTID = id };
            ViewBag.ParentName = id == 0 ? "" : _SystemCodeBLL.GetEntityByID(id).SYSTEMCODE_NAME;
            return View(detail);
        }
        /// <summary>
        /// 添加数据操作
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Createing(PROJECT_SYSTEMCODE detail)
        {
            string statuMessage = string.Empty;
            string statuCode = string.Empty;
            string exeType = string.Empty;
            if (ModelState.IsValid)
            {
                if (detail != null)
                {
                    if (detail.SYSTEMCODE_ID != 0)//实现修改
                    {
                        bool Editflag = false;
                        if (_SystemCodeBLL.GetEntityByID(detail.SYSTEMCODE_ID).SYSTEMCODE_INTACTCODE == detail.SYSTEMCODE_INTACTCODE)
                            Editflag = true;
                        else if (_SystemCodeBLL.GetEntityByID(detail.SYSTEMCODE_ID).SYSTEMCODE_INTACTCODE != detail.SYSTEMCODE_INTACTCODE && _SystemCodeBLL.GetCountByIntactCode(detail.SYSTEMCODE_INTACTCODE) <= 0)
                            Editflag = true;
                        else if (_SystemCodeBLL.GetCountByIntactCode(detail.SYSTEMCODE_INTACTCODE) > 0)
                        {
                            statuCode = "300";
                            statuMessage = "更新失败，完全编码已存在！";
                        }
                        if (_SystemCodeBLL.EditSystemCode(detail) && Editflag)
                        {
                            statuCode = "200";
                            statuMessage = "更新成功！";
                        }
                        else if (Editflag)
                        {
                            statuCode = "300";
                            statuMessage = "更新失败，请重试！";
                        }
                        exeType = "Edit";
                    }
                    else//实现添加
                    {
                        int tempNoteLevel = _SystemCodeBLL.GetEntityByID(detail.SYSTEMCODE_PARENTID ?? 0).SYSTEMCODE_NODELEVEL ?? 0;
                        detail.SYSTEMCODE_NODELEVEL = tempNoteLevel + 1;
                        if (_SystemCodeBLL.GetCountByIntactCode(detail.SYSTEMCODE_INTACTCODE) <= 0)
                        {
                            detail.SYSTEMCODE_ID = _SystemCodeBLL.AddSystemCode(detail);
                            if (detail.SYSTEMCODE_ID > 0)
                            {
                                statuCode = "200";
                                statuMessage = "添加成功！";
                            }
                            else
                            {
                                statuCode = "300";
                                statuMessage = "添加失败，请检查数据！";
                            }
                        }
                        else
                        {
                            statuCode = "300";
                            statuMessage = "添加失败，完全编码已存在！";
                        }
                        exeType = "Create";
                    }
                }
            }
            return Json(new ViewModels.CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = exeType,
                data = detail
            });
        }
        /// <summary>
        /// 修改视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditSystemCode(int id)
        {
            PROJECT_SYSTEMCODE detail = _SystemCodeBLL.GetEntityByID(id);
            ViewBag.ParentName = detail.SYSTEMCODE_PARENTID == null || detail.SYSTEMCODE_PARENTID == 0
                                     ? "" : _SystemCodeBLL.GetEntityByID(detail.SYSTEMCODE_PARENTID ?? 0).SYSTEMCODE_NAME;
            return View("Create", detail);
        }
        //删除的处理
        [HttpPost]
        public ActionResult Delete(int id)
        {
            string statuCode;
            string statuMessage;
            if (_SystemCodeBLL.DeleteSystemCode(id))
            {
                if (_SystemCodeBLL.GetNoteNumByParentID(id) > 0)//删除所有子节点
                    _SystemCodeBLL.DeleteSystemCodeByParentID(id);
                statuCode = "200";
                statuMessage = "删除成功！";
            }
            else
            {
                statuCode = "300";
                statuMessage = "删除失败，请重试！";
            }
            return Json(new KingRoad.EPM.Web.ViewModels.CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "delete"
            });
        }
        /// <summary>
        /// 详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            PROJECT_SYSTEMCODE detail = _SystemCodeBLL.GetEntityByID(id);
            ViewBag.ParentName = detail.SYSTEMCODE_PARENTID == null || detail.SYSTEMCODE_PARENTID == 0 ?"": _SystemCodeBLL.GetEntityByID(detail.SYSTEMCODE_PARENTID ?? 0).SYSTEMCODE_NAME;
            ViewBag.type = detail.SYSTEMCODE_INTACTCODE.Length > 7 ? "细目" : GettypeByNotelevel(detail.SYSTEMCODE_NODELEVEL ?? 0);
            return View(detail);
        }
        /// <summary>
        /// 获取类型
        /// </summary>
        /// <param name="Notelevel"></param>
        /// <returns></returns>
        public string GettypeByNotelevel(int Notelevel)
        {
            string tempStr = string.Empty;
            switch (Notelevel)
            {
                case 1:
                    tempStr = "部分";
                    break;
                case 2:
                    tempStr = "项"; break;
                case 3:
                    tempStr = "目"; break;
                case 4:
                    tempStr = "节"; break;
                case 5:
                    tempStr = "细目"; break;
            }
            return tempStr;
        }
    }
}
